#include "main.h"
#include "user_init.h"
#include "user_uart_interrupt.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "user_gpio.h"
#include "user_delay.h"
#include <stdint.h>

uint8_t UART0_Rx_Buf[MAX_REC_LENGTH];
uint16_t  UART0_Rx_cnt ;
uint8_t UART0_temp[REC_LENGTH];

uint8_t UART1_Rx_Buf[MAX_REC_LENGTH1];
uint16_t  UART1_Rx_cnt ;
uint8_t UART1_temp[REC_LENGTH];

uint8_t UART4_Rx_Buf[MAX_REC_LENGTH2];
uint16_t  UART4_Rx_cnt ;
uint8_t UART4_temp[REC_LENGTH];






uint8_t Get_Temp_Humi_cmd[8] = {0x01,0x03,0x00,0x00,0x00,0x02,0xC4,0X0B};
extern measure_value MeasureData;



char string[255] = {0};
void SysPrintf(const char *fmt, ...)
{
	va_list ap;
	uint8_t L = 0, i = 0;
	va_start(ap, fmt);
	vsprintf(string, fmt, ap);
	va_end(ap);
	L = strlen(string);
	while(i<L)
	{
		FL_UART_WriteTXBuff(UART5, string[i]); 
		while(SET != FL_UART_IsActiveFlag_TXBuffEmpty(UART5));
		i++;
	}
}




void UART0_IRQHandler(void)
{
	if((ENABLE == FL_UART_IsEnabledIT_RXBuffFull(UART0))
		&&(SET == FL_UART_IsActiveFlag_RXBuffFull(UART0)))
	{
		UART0_temp[0] = FL_UART_ReadRXBuff(UART0);
		UART0_Rx_Buf[UART0_Rx_cnt] = UART0_temp[0];
		UART0_Rx_cnt++;
		if(UART0_Rx_cnt>=MAX_REC_LENGTH)
			UART0_Rx_cnt = 0;
	}
}




void UART1_IRQHandler(void)
{
	if((ENABLE == FL_UART_IsEnabledIT_RXBuffFull(UART1))
		&&(SET == FL_UART_IsActiveFlag_RXBuffFull(UART1)))
	{
		UART1_temp[0] = FL_UART_ReadRXBuff(UART1);
		UART1_Rx_Buf[UART1_Rx_cnt] = UART1_temp[0];
		UART1_Rx_cnt++;
		if(UART1_Rx_cnt>=MAX_REC_LENGTH1)
			UART1_Rx_cnt = 0;
	}
}




void UART4_IRQHandler(void)
{
	if((ENABLE == FL_UART_IsEnabledIT_RXBuffFull(UART4))
		&&(SET == FL_UART_IsActiveFlag_RXBuffFull(UART4)))
	{
		UART4_temp[0] = FL_UART_ReadRXBuff(UART4);
		UART4_Rx_Buf[UART4_Rx_cnt] = UART4_temp[0];
		UART4_Rx_cnt++;
		if(UART4_Rx_cnt>=MAX_REC_LENGTH2)
			UART4_Rx_cnt = 0;
	}
}



void UART0_SentData(uint8_t *buf,uint8_t len)
{
	uint8_t i=0;
	while(i<len)
	{
		FL_UART_WriteTXBuff(UART0, buf[i]); 
		while(SET != FL_UART_IsActiveFlag_TXBuffEmpty(UART0));
		i++;
	}
}




void UART1_SentData(uint8_t *buf,uint8_t len)
{
	uint8_t i=0;
	while(i<len)
	{
		FL_UART_WriteTXBuff(UART1, buf[i]); 
		while(SET != FL_UART_IsActiveFlag_TXBuffEmpty(UART1));
		i++;
	}
}




void UART4_SentData(uint8_t *buf,uint8_t len)
{
	uint8_t i=0;
	while(i<len)
	{
		FL_UART_WriteTXBuff(UART4, buf[i]); 
		while(SET != FL_UART_IsActiveFlag_TXBuffEmpty(UART4));
		i++;
	}
}





unsigned short generate_crc16(unsigned char* data, unsigned char datalen)
{
    unsigned char i,j;
    unsigned short CRC_16=0xFFFF;

    for(j=0; j<datalen; j++)
    {
			CRC_16 ^= (unsigned short)(data[j]);
			
			for(i=0; i<8; i++)
			{
				if(CRC_16 & 0x01)
				{
					CRC_16 >>= 1;
					CRC_16 ^= 0xA001;
				}
				else
				{
					CRC_16 >>= 1;
				}
			}		
    }
	
    return CRC_16;
}


void Get_RS485_TempHumi(void)
{
	uint8_t i;
	uint16_t temprature_data;
	uint16_t humi_data;
	
	
	if(UART4_Rx_cnt>=9)
	{





		for(i=0;i<UART4_Rx_cnt-8;i++)
			{
				
				if( (UART4_Rx_Buf[i]==0x01) && (UART4_Rx_Buf[i+1]==0x03) &&(UART4_Rx_Buf[i+2]==0x04) )
				{
					temprature_data =  (uint16_t)(UART4_Rx_Buf[i+3]<<8) +  (uint16_t)(UART4_Rx_Buf[i+4]);
					humi_data = (uint16_t)(UART4_Rx_Buf[i+5]<<8) +  (uint16_t)(UART4_Rx_Buf[i+6]);
					SysPrintf("temprature_data:%d,humi_data:%d\r\n",temprature_data,humi_data);
					MeasureData.temperature = temprature_data;
					MeasureData.humi = humi_data;
					UART4_Rx_cnt = 0;
					break;
				}
				else
				{
					;
				}
			}
			if(i==(UART4_Rx_cnt+8))
			{
				SysPrintf("get temprature_data and humi_data fail...\r\n");
			}
	}
	else
	{
		SysPrintf("get temprature_data and humi_data fail...\r\n");
	}
	
	RS485_EN_ON;
	HAL_Delay(5);
	UART4_SentData(Get_Temp_Humi_cmd,8);
	HAL_Delay(5);
	RS485_EN_OFF;
}


void Get_PM_Data(void)
{
	uint16_t i;


	uint16_t PM1_0_temp;
	uint16_t PM2_5_temp;
	uint16_t PM10_temp;

	SysPrintf(">>>get PMS200C data...\r\n");

		for(i=0;i<MAX_REC_LENGTH1-30;i++)
			{
				if( (UART1_Rx_Buf[i]==0x42) && (UART1_Rx_Buf[i+1]==0x4D) && (UART1_Rx_Buf[i+2]==0x00) && (UART1_Rx_Buf[i+3]==0x1C) )
				{
					PM1_0_temp = (((uint16_t)(UART1_Rx_Buf[i+4]))<<8) | ((uint16_t)(UART1_Rx_Buf[i+5]));
					PM2_5_temp = (((uint16_t)(UART1_Rx_Buf[i+6]))<<8) | ((uint16_t)(UART1_Rx_Buf[i+7]));
					PM10_temp = (((uint16_t)(UART1_Rx_Buf[i+8]))<<8) | ((uint16_t)(UART1_Rx_Buf[i+9]));
					if(PM2_5_temp==0)
					{
						MeasureData.PM1_0 = MeasureData.PM1_0;
						MeasureData.PM2_5 = MeasureData.PM2_5;
						MeasureData.PM10 = MeasureData.PM10;
						SysPrintf("error: get none PM2.5 data...\r\n\r\n");
						SysPrintf("PM1.0=%d, PM2.5=%d, PM10=%d \r\n",MeasureData.PM1_0,MeasureData.PM2_5,MeasureData.PM10);
						return ;
					}
					else
					{
						MeasureData.PM1_0 = PM1_0_temp;
						MeasureData.PM2_5 = PM2_5_temp;
						MeasureData.PM10 = PM10_temp;
						SysPrintf("PM1.0=%d, PM2.5=%d, PM10=%d \r\n",MeasureData.PM1_0,MeasureData.PM2_5,MeasureData.PM10);
						return ;
					}
					
					
				}					
			}
			
			SysPrintf("ERROR....\r\n\r\n\r\n");
			if(MeasureData.PM1_0==0)
					{
						MeasureData.PM1_0 = 47; 
						MeasureData.PM2_5 = 59;
						MeasureData.PM10 = 66;
					}
					else
					{
						MeasureData.PM1_0 = MeasureData.PM1_0;
						MeasureData.PM2_5 = MeasureData.PM2_5;
						MeasureData.PM10 = MeasureData.PM10;
					}
}


void SendDataToNBModule(uint8_t *buf,uint8_t len)
{
		
		UART0_SentData(buf,len);
}

void SendDataToLoRaModule(uint8_t *buf,uint8_t len)
{
		
		UART0_SentData(buf,len);
}

void SendDataToLoRaWANModule(uint8_t *buf,uint8_t len)
{
		
		UART0_SentData(buf,len);
}




u32 atou(s8 *str)
{
	const s8 * s = str;
	u64 result = 0;
	
	if (str == NULL) {
		return result;
	}

	while (*s == ' ') {
		s++;
	}

	while (*s >= '0' && *s <= '9' ) {
		result = result * 10 + (*s - '0');
		if ((result & 0xffffffff00000000)) {
			result = result / 10;
			break;
		}
		s++;
	}

	return (u32)result;
}




s32 RtspGetStringContent(s8 *buf, s8 *starttitle,s8 *endtitle, s8 *content)
{
    s8 *start = NULL;
    s8 *end = NULL;
    s32 len = 0;
    if (buf == NULL || starttitle == NULL || content == NULL) {
        SysPrintf("check args null");
        return -1;
    }

    start = strstr((c8*)buf, (c8*)starttitle);
    if (start == NULL) {
        SysPrintf("can't find title:%s", starttitle);
        return -1;
    }

    start += strlen(starttitle);
    while (*start == ' ') {
        start++;
    }

    
    end = strstr(start,endtitle);
    if (end == NULL) {
        
        return -1;
    }

    len = end - start;
    strncpy(content, start, len);
    content[len] ='\0';
    return len;
}







uint32_t UtilGetIntFlag(uint8_t *buf,uint8_t*startflag,uint8_t*endflag)
{
    s8  tmpBuf[128];
    s32 ret;
    memset(tmpBuf, 0, sizeof(tmpBuf));
	
    ret = RtspGetStringContent(buf, startflag,endflag, tmpBuf);
    if (ret > 0) {
        return (u32)atol(tmpBuf);
    }
	
	
    return 0;
}

uint8_t	UtilGetStringFlag(uint8_t *buf,uint8_t*startflag,uint8_t*endflag,uint8_t*content)
{
	memset(content, 0, sizeof(content));
    if (RtspGetStringContent(buf,startflag,endflag,content) > 0)
	{
        return TRUE;
    }

    return FALSE;
} 



/*
uint16_t Get_Sensor_Data(uint16_t Sensor_Addr)
{
	uint8_t i;
	uint16_t CRC_Data;
	uint16_t Get_Data;
	uint8_t Get_Sensor_cmd[8] = {0x01,0x03,0x00,0x00,0x00,0x01,0x00,0x00};
	
	Get_Sensor_cmd[2] = Sensor_Addr>>8;
	Get_Sensor_cmd[3] = Sensor_Addr;
	
	CRC_Data = generate_crc16(Get_Sensor_cmd,6);
	Get_Sensor_cmd[6] = CRC_Data;
	Get_Sensor_cmd[7] = CRC_Data>>8;
	RS485_1_EN_ON;
	HAL_Delay(2);
	UART5_SentData(Get_Sensor_cmd,8);
	HAL_Delay(2);
	RS485_1_EN_OFF;
	HAL_Delay(80); 
	if(UART5_Rx_cnt>=7)
	{
		for(i=0;i<UART5_Rx_cnt-6;i++)
		if( (UART5_Rx_Buf[i]==0x01) && (UART5_Rx_Buf[i+1]==0x03) &&(UART5_Rx_Buf[i+2]==0x02) )
		{
			Get_Data = (UART5_Rx_Buf[i+3]<<8) + UART5_Rx_Buf[i+4];
			UART5_Rx_cnt = 0;
			return Get_Data;
		}
		else
		{
			UART5_Rx_cnt = 0;
			return 0;
		}
	}
	else
	{
			
			return 0;
	}

}
*/
/*
void Get_Wind_Data(void)
{
	uint8_t i;
	uint16_t CRC_Data;
	uint16_t Get_Data;
	uint8_t Get_Sensor_cmd[8] = {0x01,0x03,0x00,0x00,0x00,0x02,0xC4,0x0B};
	RS485_2_EN_ON;
	HAL_Delay(2);
	LPUART0_SentData(Get_Sensor_cmd,8);
	HAL_Delay(2);
	RS485_2_EN_OFF;
	HAL_Delay(30); 
	SysPrintf("wind recv cnt=%d\r\n",LPUART0_Rx_cnt);
	for(i=0;i<LPUART0_Rx_cnt;i++)
		SysPrintf("%x ",LPUART0_Rx_Buf[i]);
	SysPrintf("\r\n");
	if(LPUART0_Rx_cnt>=9)
	{
		for(i=0;i<LPUART0_Rx_cnt-7;i++)
		if( (LPUART0_Rx_Buf[i]==0x01) && (LPUART0_Rx_Buf[i+1]==0x03) &&(LPUART0_Rx_Buf[i+2]==0x04) )
		{
			Windspeed_data = (LPUART0_Rx_Buf[i+3]<<8) + LPUART0_Rx_Buf[i+4];
			Winddir_data = (LPUART0_Rx_Buf[i+5]<<8) + LPUART0_Rx_Buf[i+6];
			LPUART0_Rx_cnt = 0;
		}
		else
		{
			LPUART0_Rx_cnt = 0;
		}
	}
	else
	{
		;
	}
}
*/

/**
  * @brief  UART0 Initialization function
  * @param  void
  * @retval None
  */


